package com.haoshuang.zhouzhoubang.ui.page.order.scan.inputcode

import android.content.Context
import android.os.Bundle
import android.widget.Toast
import com.haoshuang.zhouzhoubang.databinding.ActivityInputCodeBinding
import com.haoshuang.zhouzhoubang.databinding.ActivityScanBinding
import com.haoshuang.zhouzhoubang.ui.page.order.appeal.AppealViewModel
import com.haoshuang.zhouzhoubang.ui.page.order.scan.ScanActivity
import com.haoshuang.zhouzhoubang.ui.page.order.scan.ScanViewModel
import com.haoshuang.zhouzhoubang.ui.page.order.scan.writeofforder.WriteOffOrderActivity
import com.haoshuang.zhouzhoubang.ui.page.order.scan.writeoffresult.WriteOffResultActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 * 输入券码
 */
class InputCodeActivity : BaseVMActivity<ScanViewModel, ActivityInputCodeBinding>() {

    companion object {

        fun launch(
            context: Context
        ) {
            context.startPage<InputCodeActivity>()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding?.ivScan?.clickWithTrigger {
            ScanActivity.launch(this)
            finish()
        }

        binding?.tvSearch?.clickWithTrigger {
            if (binding?.editHexiaoma?.text?.trim().isNullOrEmpty()){
                toast("请输入核销码")
                return@clickWithTrigger
            }
            var result = binding?.editHexiaoma?.text?.trim()?.toString()
            vm.getHexiaoOrderInfo(result)

//            WriteOffOrderActivity.launch(this@InputCodeActivity)
        }
    }

    override fun initData() {
        vm.getHexiaoOrderInfoData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
//                toast(msg)
                WriteOffResultActivity.launch(this@InputCodeActivity,false,msg)
                finish()
            }
            onSuccess {
                dismissLoading()
                if (it?.data != null) {
                    WriteOffOrderActivity.launch(this@InputCodeActivity, it.data!!)
                    finish()
                }

            }
        }
    }
}